import Mock from 'mockjs';

// 表格
const StudentData = Mock.mock({
    'data|100': [
        { 'id|+1': 1, 'name': '@name', 'address': '@region', 'score|70-100': 100 }
    ]
});
// 查
export let userQuery = (data)=>{
    let resData = [],
        queryParam = JSON.parse(data.body);
    resData = StudentData.data.filter(item => {
        return item.id.toString().includes(queryParam.id)
    })

    const resMsg = {
        'code': 200,
        'data': resData,
        'msg': '查询成功'
    }
    return resMsg
}
// 改
export let userUpdate = (data)=>{
    let student = JSON.parse(data.body)
    student.id = parseInt(student.id)
    student.score = parseInt(student.score)
    for (let index in StudentData.data) {
        if (StudentData.data[index].id === student.id) {
            StudentData.data[index] = student
        }
    }
    const resMsg = {
        'code': 200,
        'msg': '修改成功'
    }
    return resMsg
}
// 添加
export let userAdd = (data)=>{
    let student = JSON.parse(data.body)
    student.id = parseInt(student.id)
    student.score = parseInt(student.score)
    StudentData.data.push(student)
    const resMsg = {
        'code': 200,
        'msg': '添加成功'
    }
    return resMsg
}
// 删除
export let userDelete = (data)=>{
    let id = parseInt(JSON.parse(data.body))
    for (let index in StudentData.data) {
        if (StudentData.data[index].id === id) {
            StudentData.data.splice(index, 1)
            break
        }
    }
    const resMsg = {
        'code': 200,
        'msg': '删除成功'
    }
    return resMsg
}


